package com.example.myapplication.ui.fragment;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.bean.ImageBean;
import com.example.myapplication.database.table.ImageDatabase;
import com.example.myapplication.ui.adapter.ImageFragmentPagerAdapter;
import com.example.myapplication.util.ContentUtils;
import com.google.gson.Gson;


import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ImageFragment extends BaseFragment {

    ViewPager imgVp;
    private List<ImageBean.ResultBean> imgList;
//    当ViewPager显示的内容为Fragment时，将要显示的Fragment放在一个集合当中进行管理
    List<Fragment>fragmentList;
    private ImageFragmentPagerAdapter adapter;
    ImageDatabase database;
    SQLiteDatabase db;
    Context context;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_style, container, false);
        context=getContext();
        imgVp = view.findViewById(R.id.frag_vp);
        fragmentList = new ArrayList<>();
        loadData(getContext(), ContentUtils.PICTURE_URL,ContentUtils.FILE_IMAGE,false,true,false);
        return view;
    }

    @Override
    public void showDataFromNet(String s) {
        ImageBean imageBean = new Gson().fromJson(s, ImageBean.class);
        imgList = imageBean.getResult();
        for (int i = 0; i < imgList.size(); i++) {
            Fragment itemImgFrag = new ItemImageFragment();
//            传递对象到包含的fragment里面
            ImageBean.ResultBean bean = imgList.get(i);
            Bundle bundle = new Bundle();
            bundle.putSerializable("image",bean);
            itemImgFrag.setArguments(bundle);
            fragmentList.add(itemImgFrag);
        }
//        设置适配器
        adapter = new ImageFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        imgVp.setAdapter(adapter);
    }

}
